package com.hns.frame.util;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hns.frame.enumrate.MsgType;

/**
 * 返回数据 工具类
 * @author:Fisher
 * @email:zhuangcaijin@126.com
 * @version Revision 2.0.0
 */
public class ResponseUtil {
    
    /**
     * 请求错误时返回
     * @param errorMsg  错误信息
     * @param errorType 错误类别
     * @param request
     * @param response
     * @return  返回map
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     */
    public static String error(String errorMsg, String errorType,HttpServletRequest request,HttpServletResponse response) {
        HashMap<String, Object> result = new HashMap<String, Object>();
        result.put("msgType", MsgType.ERROR.getFlag());
        result.put("errorMsg", errorMsg);
        responseJSON(result,response);
        return null;
    }
    
    /**
     * session过期时返回
     * @param request
     * @param response
     * @return  session过期map
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     */
    public static String sessionExpired(HttpServletRequest request,HttpServletResponse response) {
        HashMap<String, Object> result = new HashMap<String, Object>();
        result.put("sessionExpired", true);
        responseJSON(result,response);
        return null;
    }
    
    /**
     * 响应返回json格式的对象
     * @param result    需要返回的对象
     * @param response
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     */
    public static void responseJSON(Object result,HttpServletResponse response) {
        PrintWriter out = null;
        try {
            /* 
             * 在调用getWriter之前未设置编码(既调用setContentType或者setCharacterEncoding方法设置编码), 
             * HttpServletResponse则会返回一个用默认的编码(既ISO-8859-1)编码的PrintWriter实例。这样就会 
             * 造成中文乱码。而且设置编码时必须在调用getWriter之前设置,不然是无效的。 
             * */  
            response.setContentType("text/html;charset=utf-8");
            //response.setCharacterEncoding("UTF-8");
            out = response.getWriter();
            out.println(returnJsonStr(result));
            out.flush();
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            if(out!=null){
                out.close();
            }
        }
    }
    
    /**
     * 返回json字符串
     * @param o 对象
     * @return  转化为字符串后的字符串
     * @throws IOException
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     */
    public static String returnJsonStr(Object o) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        StringWriter writer = new StringWriter();
        JsonGenerator gen = objectMapper.getJsonFactory().createJsonGenerator(writer);
        objectMapper.writeValue(gen, o);
        gen.close();
        writer.close();
        return writer.toString();
    }
}
